//log=console.log;

$(document).ready(function() { 

        /*
        * Initialze datepicker calendar
        * 
        */
        init_dates = function(){
            /***********************************************************
            * JQUERY UI Calendar with event
            ***********************************************************/

            //console.log('init');

            var date = new Date();    

            //  limiter 1 WEBSITE PICUP DATE - today is not available
            //  limiter 0 SYSTEM PICUP DATE - today is available
            var limiter = 1;

            if(typeof(user_name) != "undefined"){
                limiter = 0;   
            }

            var m = date.getMonth(),
            d = date.getDate() + limiter,
            y = date.getFullYear();

            // Disable all dates till today
            $('#datefrom').datepicker({
                    minDate: new Date(y, m, d),
                    dateFormat: 'dd.mm.yy',
                    showOn: 'both',
                    buttonImage: base_url+"assets/img/icon-datepicker.png",
                    buttonImageOnly: true,
                    nextText: '&raquo;',
                    prevText: '&laquo;',
                    onSelect: function(dateStr) {

                        // get selected date
                        var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);

                        // set min date to dateto to enable selecting date
                        $('#dateto').datepicker("option", "minDate", depart);

                        // selected date + 1 day
                        depart.setDate(depart.getDate() + limiter);

                        // set date to
                        $('#dateto').datepicker("setDate", depart);
                        $('#dateto').datepicker("option", "minDate", depart);

                        // set hidden elements
                        $('#pickup_date').val($.datepicker.formatDate('dd.mm.yy', $('#datefrom').datepicker('getDate')));
                        $('#return_date').val($.datepicker.formatDate('dd.mm.yy', $('#dateto').datepicker('getDate')));

                        if(method=='car_details' || method=='browse_car_details')
                            {

                            // get selected date
                            var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);

                            // set datefrom2 sam as datefrom
                            $('#datefrom2').datepicker("setDate", depart );

                            // set dateto2 sam as dateto
                            var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);

                            // set min date to dateto to enable selecting date
                            $('#dateto2').datepicker("option", "minDate", depart);

                            depart.setDate(depart.getDate() + limiter);

                            $('#dateto2').datepicker("setDate", depart);
                            $('#dateto2').datepicker("option", "minDate", depart);

                        }

                    }


            });

            $('#dateto').datepicker({
                    minDate: new Date(y, m, d),
                    dateFormat: 'dd.mm.yy',
                    showOn: 'both',
                    buttonImage: base_url+"assets/img/icon-datepicker.png",
                    buttonImageOnly: true,
                    nextText: '&raquo;',
                    prevText: '&laquo;',
                    onSelect: function(dateStr) {
                        $('#return_date').val($.datepicker.formatDate('dd.mm.yy', $('#dateto').datepicker('getDate')));

                        if(method=='car_details' || method=='browse_car_details')
                            {
                            // get selected date
                            var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);
                            $('#dateto2').datepicker("setDate", depart);
                        }

                    }
            })

            // SET TIME & DATE
            $('#fullfrom').val('08:00');
            $('#fullto').val('08:00');

            $('#datefrom').datepicker('setDate', new Date(y, m, d));
            $('#dateto').datepicker('setDate', new Date(y, m, d+1));

            $('#pickup_date').val($.datepicker.formatDate('dd.mm.yy', $('#datefrom').datepicker('getDate')));
            $('#return_date').val($.datepicker.formatDate('dd.mm.yy', $('#dateto').datepicker('getDate')));

            //alert(pickup_date!="")
            if(pickup_date!="")
                {
                //set pickupdate from session
                var n1=pickup_date.split("."); 
                $('#datefrom').datepicker('setDate', new Date(n1[2], n1[1]-1, n1[0]));

                //set returndate from session
                var n2=return_date.split("."); 
                $('#dateto').datepicker('setDate', new Date(n2[2], n2[1]-1, n2[0])); 

                //set pickup and return locations and times
                $('#pickup_loc_id').val(pickup_loc_id);
                $('#return_loc_id').val(return_loc_id);

                $('#fullfrom').val(fullfrom);
                $('#fullto').val(fullto);

                $('#pickup_date').val($.datepicker.formatDate('dd.mm.yy', $('#datefrom').datepicker('getDate')));
                $('#return_date').val($.datepicker.formatDate('dd.mm.yy', $('#dateto').datepicker('getDate')));

            }

        }

        /*
        * Submits extras form
        */
        submit_extras = function(elem)
        {
            extras_url = base_url+lang+'/rentacar/set_extras';

            elem.busy();

            data = $('#car_accasories').serialize();
            $.ajax({
                    dataType: 'JSON',
                    type: 'POST',
                    data: data,
                    url: extras_url,
                    onerror: null,
                    success: function(data){

                        if(data.action==true){
                            elem.busy('hide');
                            $('.bClose').trigger('click');
                            $('#extras_html').html(data.html);
                            $('#final_price').html(data.tot_price+"€");

                        }else{
                            alert('Error ocured while adding extras.')
                            elem.busy('hide');
                        }            

                    }
            });

        }

        /*
        * Initialze datepicker calendar
        * 
        */
        init_dates2 = function(){
            /***********************************************************
            * JQUERY UI Calendar with event
            ***********************************************************/

            //console.log('init');

            var date = new Date();    

            //  limiter 1 WEBSITE PICUP DATE - today is not available
            //  limiter 0 SYSTEM PICUP DATE - today is available
            var limiter = 1;

            if(typeof(user_name) != "undefined"){
                limiter = 0;   
            }

            var m = date.getMonth(),
            d = date.getDate() + limiter,
            y = date.getFullYear();

            // Disable all dates till today
            $('#datefrom2').datepicker({
                    minDate: new Date(y, m, d),
                    dateFormat: 'dd.mm.yy',
                    showOn: 'both',
                    buttonImage: base_url+"assets/img/date-bgr.png",
                    buttonImageOnly: true,
                    nextText: '&raquo;',
                    prevText: '&laquo;',
                    onSelect: function(dateStr) {

                        var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);

                        $('#dateto2').datepicker("option", "minDate", depart);
                        depart.setDate(depart.getDate() + limiter);
                        $('#dateto2').datepicker("setDate", depart);
                        $('#dateto2').datepicker("option", "minDate", depart);

                        // get selected date
                        var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);

                        // set datefrom2 sam as datefrom
                        $('#datefrom').datepicker("setDate", depart );

                        // set dateto2 sam as dateto
                        var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);

                        // set min date to dateto to enable selecting date
                        $('#dateto').datepicker("option", "minDate", depart);

                        depart.setDate(depart.getDate() + limiter);

                        $('#dateto').datepicker("setDate", depart);
                        $('#dateto').datepicker("option", "minDate", depart);

                        $('#pickup_date').val($.datepicker.formatDate('dd.mm.yy', $('#datefrom').datepicker('getDate')));
                        $('#return_date').val($.datepicker.formatDate('dd.mm.yy', $('#dateto').datepicker('getDate')));




                    }
            });

            $('#dateto2').datepicker({
                    minDate: new Date(y, m, d),
                    dateFormat: 'dd.mm.yy',
                    showOn: 'both',
                    buttonImage: base_url+"assets/img/date-bgr.png",
                    buttonImageOnly: true,
                    nextText: '&raquo;',
                    prevText: '&laquo;',
                    onSelect: function(dateStr) {
                        $('#return_date').val($.datepicker.formatDate('dd.mm.yy', $('#dateto').datepicker('getDate')));

                        // get selected date
                        var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);
                        $('#dateto').datepicker("setDate", depart);
                        
                        $('#pickup_date').val($.datepicker.formatDate('dd.mm.yy', $('#datefrom').datepicker('getDate')));
                        $('#return_date').val($.datepicker.formatDate('dd.mm.yy', $('#dateto').datepicker('getDate')));

                    }
            })

            // SET TIME & DATE
            $('#fullfrom2').val('08:00');
            $('#fullto2').val('08:00');

            $('#datefrom2').datepicker('setDate', new Date(y, m, d));
            $('#dateto2').datepicker('setDate', new Date(y, m, d+1));

            //alert(pickup_date!="")
            if(pickup_date!="")
                {
                //set pickupdate from session
                var n1=pickup_date.split("."); 
                $('#datefrom2').datepicker('setDate', new Date(n1[2], n1[1]-1, n1[0]));

                //set returndate from session
                var n2=return_date.split("."); 
                $('#dateto2').datepicker('setDate', new Date(n2[2], n2[1]-1, n2[0])); 

                //set pickup and return locations and times
                $('#pickup_loc_id2').val(pickup_loc_id).trigger('change');
                $('#return_loc_id2').val(return_loc_id).trigger('change');

                $('#fullfrom2').val(fullfrom).trigger('change');
                $('#fullto2').val(fullto).trigger('change');;

            }

        }

});